package com.controller;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.beans.Book;
import com.beans.Company;
import com.beans.User;
import com.service.SysManager;
import com.util.StringUtil;
import com.yeepay.Configuration;
import com.yeepay.PaymentForOnlineService;

public class BookController extends UniversalController {
	private SysManager sysManager;

	public SysManager getSysManager() {
		return sysManager;
	}

	public void setSysManager(SysManager sysManager) {
		this.sysManager = sysManager;
	}

	public ModelAndView getPay(HttpServletRequest request,
			HttpServletResponse response) throws IllegalAccessException,
			InvocationTargetException {
		User u = (User) request.getSession().getAttribute("user");
		Company c = (Company) request.getSession().getAttribute("company");
		Map map = new HashMap();
		if (u != null)
			map.put("bookID", StringUtil.bookID(u.getId() + ""));
		else if (c != null)
			map.put("bookID", StringUtil.bookID(c.getId() + ""));
		else
			return new ModelAndView("login");
		return new ModelAndView("yeepay/pay",map);
	}
	public ModelAndView getReqPay(HttpServletRequest request,
			HttpServletResponse response) throws IllegalAccessException,
			InvocationTargetException {
		User u = (User) request.getSession().getAttribute("user");
		Company c = (Company) request.getSession().getAttribute("company");
		String p2_Order=request.getParameter("p2_Order");
		String p3_Amt=request.getParameter("p3_Amt");
		double money=Double.parseDouble(p3_Amt);
		Book book = new Book();
		book.setBookID(p2_Order);
		book.setMoney(money);
		book.setState("0");
		book.setIp(request.getRemoteAddr());
		if (u != null) {
			book.setUserID(u.getId());
		} else if (c != null) {
			book.setCompanyID(c.getId());
		} else
			return new ModelAndView("login");
		Book b=(Book)sysManager.findByID(Book.class, p2_Order);
		if(b!=null)sysManager.delete(b);
		sysManager.insert(book);
		return new ModelAndView("yeepay/reqpay");
	}
	public ModelAndView getCallBack(HttpServletRequest request,
			HttpServletResponse response) throws IllegalAccessException,
			InvocationTargetException, UnsupportedEncodingException {
//		User u = (User) request.getSession().getAttribute("user");
//		Company c = (Company) request.getSession().getAttribute("company");
		String msg="";
//		Map map = new HashMap();
//		if (u != null)
//			;
//		else if (c != null)
//			;
//		else
//			return new ModelAndView("login");
		String keyValue   = StringUtil.formatString(Configuration.getInstance().getValue("keyValue"));   // 商家密钥
		String r0_Cmd 	  = StringUtil.formatString(request.getParameter("r0_Cmd")); // 业务类型
		String p1_MerId   = StringUtil.formatString(Configuration.getInstance().getValue("p1_MerId"));   // 商户编号
		String r1_Code    = StringUtil.formatString(request.getParameter("r1_Code"));// 支付结果
		String r2_TrxId   = StringUtil.formatString(request.getParameter("r2_TrxId"));// 易宝支付交易流水号
		String r3_Amt     = StringUtil.formatString(request.getParameter("r3_Amt"));// 支付金额
		String r4_Cur     = StringUtil.formatString(request.getParameter("r4_Cur"));// 交易币种
		String r5_Pid     = StringUtil.formatString(request.getParameter("r5_Pid"));// 商品名称
		String r6_Order   = StringUtil.formatString(request.getParameter("r6_Order"));// 商户订单号
		String r7_Uid     = StringUtil.formatString(request.getParameter("r7_Uid"));// 易宝支付会员ID
		String r8_MP      = StringUtil.formatString(request.getParameter("r8_MP"));// 商户扩展信息
		String r9_BType   = StringUtil.formatString(request.getParameter("r9_BType"));// 交易结果返回类型
		String hmac       = StringUtil.formatString(request.getParameter("hmac"));// 签名数据
		boolean isOK = false;
		// 校验返回数据包
		isOK = PaymentForOnlineService.verifyCallback(hmac,p1_MerId,r0_Cmd,r1_Code, 
				r2_TrxId,r3_Amt,r4_Cur,r5_Pid,r6_Order,r7_Uid,r8_MP,r9_BType,keyValue);
		if(isOK) {
			
			//在接收到支付结果通知后，判断是否进行过业务逻辑处理，不要重复进行业务逻辑处理
			if(r1_Code.equals("1")) {
				// 产品通用接口支付成功返回-浏览器重定向
				if(r9_BType.equals("1")) {
//					msg="callback方式:产品通用接口支付成功返回-浏览器重定向";
					msg="<html><head><title>支付成功！</title></head><body><h1><br>交易成功!<br>商家订单号:" + r6_Order + "<br>支付金额:" + r3_Amt + "<br>易宝支付交易流水号:" + r2_TrxId+"</h1></body></html>";
					// 产品通用接口支付成功返回-服务器点对点通讯
				} else if(r9_BType.equals("2")) {
					Book b=(Book)sysManager.findByID(Book.class, r6_Order);
					if(b!=null){
						b.setState("1");
						sysManager.update(b);
					}
					// 如果在发起交易请求时	设置使用应答机制时，必须应答以"success"开头的字符串，大小写不敏感
					msg="SUCCESS";
				  // 产品通用接口支付成功返回-电话支付返回		
				}
				// 下面页面输出是测试时观察结果使用
			}
		} else {
			msg="交易签名被篡改!";
		}
		getMessage(response, msg);
//		map.put("msg", msg);
		return null;//new ModelAndView("yeepay/callBack", map);
	}
	
}